package edu.bbu.license.client.android.gui.transfer;

import java.util.List;

import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import edu.bbu.license.client.android.R;
import edu.bbu.license.client.android.controller.transfer.TransferController;
import edu.bbu.license.client.android.gui.BaseActivity;
import edu.bbu.license.client.android.gui.transfer.adapter.FilesTransferActivityTransferAdapter;
import edu.bbu.license.client.android.gui.transfer.model.FileTransferItem;

public class FileTransferActivity extends BaseActivity implements FileTransferView {

	private ListView listView;
	private FilesTransferActivityTransferAdapter transferAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.files_transfer_activity);
		
		transferAdapter = new FilesTransferActivityTransferAdapter();
		
		listView = (ListView) findViewById(R.id.files_traffic_activity_traffic_list);
		listView.setAdapter(transferAdapter);
		
		TransferController.getInstance().atach(this);
	}
	
	@Override
	public void addTransferItem(final FileTransferItem fileTransferItem) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				transferAdapter.addItem(fileTransferItem);
			}
		});
	}
	
	@Override
	public void removeTransferItem(final FileTransferItem fileTransferItem) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				transferAdapter.removeItem(fileTransferItem);
			}
		});
		
	}

	@Override
	public void addTransferItems(final List<FileTransferItem> fileTransferItemList) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				transferAdapter.addItems(fileTransferItemList);
			}
		});
	}
	
	@Override
	public void removeTransferItems() {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				transferAdapter.removeItems();
			}
		});
		
	}
	
	@Override
	public void setTransferItemClickListener(OnItemClickListener itemClickListener) {
		listView.setOnItemClickListener(itemClickListener);
	}
	
	@Override
	public void setStartButtonListener(OnClickListener listner) {
		((ImageButton) findViewById(R.id.files_traffic_activity_start_button)).setOnClickListener(listner);
	}
	
	@Override
	public void setPauseButtonListener(OnClickListener listner) {
		((ImageButton) findViewById(R.id.files_traffic_activity_pause_button)).setOnClickListener(listner);		
	}
	
	@Override
	public void setStopButtonListener(OnClickListener listner) {
		((ImageButton) findViewById(R.id.files_traffic_activity_stop_button)).setOnClickListener(listner);
	}
	
	@Override
	public void setRemoveButtonListener(OnClickListener listner) {
		((ImageButton) findViewById(R.id.files_traffic_activity_remove_button)).setOnClickListener(listner);
	}
	
	@Override
	public void selectItem(FileTransferItem transferItem) {
		for (FileTransferItem item : transferAdapter.getFileTransferItems()) {
			item.setSelected(false);
		}
		
		transferItem.setSelected(true);
		listView.invalidate();
	}
	
	@Override
	public void deselectItem(FileTransferItem transferItem) {
		transferItem.setSelected(false);
	}
	
	@Override
	public FileTransferItem getSelectedItem() {
		for (FileTransferItem item : transferAdapter.getFileTransferItems()) {
			if(item.isSelected()) {
				return item;
			}
		}
		
		return null;		
	}
}
